package com.liqi.statistics.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.liqi.common.annotation.DataScope;
import com.liqi.statistics.domain.dto.ItemStatisticsDto;
import com.liqi.statistics.module.ItemStatisticsModule;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liqi.statistics.domain.DvServiceTime;
import com.liqi.statistics.mapper.DvServiceTimeMapper;
import com.liqi.statistics.service.DvServiceTimeService;
/**
 * @Author: lgh
 * @Date: 2022-11-05
 * @Description: 
 */
@Service
public class DvServiceTimeServiceImpl extends ServiceImpl<DvServiceTimeMapper, DvServiceTime> implements DvServiceTimeService{
    @Autowired
    private DvServiceTimeMapper dvServiceTimeMapper;
    @Override
    @DataScope(deptAlias = "d" , userAlias = "u",regionAlias = "r",groupAlias = "g")
    public List<ItemStatisticsModule> useFrequencyTow(ItemStatisticsDto dto) {
        return dvServiceTimeMapper.useFrequencyTow(dto);

    }


    @Override
    public Integer setTime(String deviceId) {
        QueryWrapper<DvServiceTime> qw = new QueryWrapper<>();

        SimpleDateFormat simpleDateFormatTime=new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat formatyyyymmddhhmmsss=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date=simpleDateFormatTime.format(new Date());

        qw.eq("device_id",deviceId );
        qw.eq("date",date);
        DvServiceTime serviceTime = dvServiceTimeMapper.selectOne(qw);

        if(serviceTime==null){
            serviceTime =new DvServiceTime();
            serviceTime.setDate(new Date());
            serviceTime.setDeviceId(deviceId);
            serviceTime.setUpdateTime(new Date());
            serviceTime.setTime(0);
            dvServiceTimeMapper.insert(serviceTime);
            return  0;
        }else {
            Date updateTime = serviceTime.getUpdateTime();
            Integer time = serviceTime.getTime();
            String dateTime = formatyyyymmddhhmmsss.format(new Date());
            String formatUpdateTime = formatyyyymmddhhmmsss.format(updateTime);
            try {
                serviceTime.setTime((int)getTime(formatUpdateTime, dateTime)+time);
            } catch (ParseException e) {
                e.printStackTrace();
            }



            serviceTime.setUpdateTime(new Date());

            dvServiceTimeMapper.update(serviceTime,qw);
            return  1;
        }


    }
    public static long getTime(String startTime, String endTime) throws ParseException {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long eTime = df.parse(endTime).getTime();
        long sTime = df.parse(startTime).getTime();
        long diff = (eTime - sTime) / 1000;
        return diff;
    }

}
